package com.commerce.addressbook.utlis;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.widget.Toast;

import java.util.List;

public class ToIntent {

    public void toIntentDIAL(Context context, String phoneNumber){
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if(ui(context,intent)){
            context.startActivity(intent);
        }

    }
    
    public void toIntentSENDTO(Context context, String phoneNumber){
        Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+phoneNumber));
        if(ui(context,intent)){
            context.startActivity(intent);
        }
    }

    private boolean ui(Context context, Intent intent){
        Boolean support = true;
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
        if(activities.size()<=0){
            Toast.makeText(context, "设备不支持", Toast.LENGTH_SHORT).show();
            support=false;
        }
       return support;
    }
}
